﻿/*
 * HCC Robotica, Workshop Robot programmeren met C# 
 * Oefening : wo_03	Drukknop acties
 * Naam     : Hinnie
 * Versie   : september 2011
 */

using System;
using System.Threading;
using System.IO.Ports;
using System.Text;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.FEZ;

namespace wo_03
{
    public class wo_03
    {
        public static void Main()
        {
            OutputPort LED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
            InputPort ButtonLDR = new InputPort((Cpu.Pin)FEZ_Pin.Digital.LDR, false, Port.ResistorMode.PullUp);
            InputPort ButtonDi2 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di2, false, Port.ResistorMode.Disabled);
            InputPort ButtonDi4 = new InputPort((Cpu.Pin)FEZ_Pin.Digital.Di4, false, Port.ResistorMode.Disabled);
            SerialPort ComPort = new SerialPort("COM2", 115200);    // TODO: Hier je eigen waardes invullen.

            bool ledState = false;
            bool ledSwitch = false;
            const int loopTime = 5;
            const int flashTime = 200 / loopTime;
            bool butLDR = true;
            bool butDi2 = true;
            bool butDi4 = true;

            ComPort.Open();
            byte[] txBuf;
            string Msg = "";
            bool tx = false;
            int flashCount = 0;
            while (true)
            {
                if (ButtonLDR.Read() != butLDR)
                {
                    butLDR = ButtonLDR.Read();
                    if (!butLDR)
                        Msg = "LDR On\n\r";
                    else
                        Msg = "LDR Off\n\r";
                    tx = true;
                }
                else if (ButtonDi2.Read() != butDi2)
                {
                    butDi2 = ButtonDi2.Read();
                    if (!butDi2)
                        Msg = "Di2 On\n\r";
                    else
                        Msg = "Di2 Off\n\r";
                    tx = true;
                }
                else if (ButtonDi4.Read() != butDi4)
                {
                    butDi4 = ButtonDi4.Read();
                    if (!butDi4)
                        Msg = "Di4 On\n\r";
                    else
                        Msg = "Di4 Off\n\r";
                    tx = true;
                }
                if (tx)
                {
                    tx = false;
                    txBuf = Encoding.UTF8.GetBytes(Msg);
                    ComPort.Write(txBuf, 0, txBuf.Length);
                }
                if (flashCount++ >= flashTime)
                {
                    flashCount = 0;
                    ledState = !ledState;       // toggle LED state
                    LED.Write(ledState);        // Blink board LED
                }
                if (ledState)
                {
                    ledSwitch = !ledSwitch;
                    LED.Write(ledSwitch);       // less intensity onboard LED
                }
                Thread.Sleep(loopTime);
            }
        }

    }
}
